package com.cloud.wms.ui.group.entity;

import com.annimon.stream.Stream;
import com.cloud.wms.ui.main.entity.CommodityEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:Chen
 * @Date: 2021-03-24 18:12
 * @Description:
 */
public class BatchCommodityEntity {

    private ArrayList<CommodityEntity> litWjx;
    private ArrayList<CommodityEntity> litYjx;

    public ArrayList<CommodityEntity> getBatchCommodity() {
        ArrayList<CommodityEntity> commodityEntities = new ArrayList<>();
        for (int i = 0; i < litYjx.size(); i++) {
            litYjx.get(i).setFlag(true);
        }
        commodityEntities.addAll(litWjx);
        commodityEntities.addAll(litYjx);
        return commodityEntities;
    }

    public List<CommodityEntity> getBatchPos() {
        ArrayList<CommodityEntity> commodityEntities = new ArrayList<>();
        for (int i = 0; i < litYjx.size(); i++) {
            litYjx.get(i).setFlag(true);
        }
        commodityEntities.addAll(litWjx);
        commodityEntities.addAll(litYjx);
        ArrayList<CommodityEntity> list = new ArrayList<>();
        Stream.ofNullable(commodityEntities)
                .groupBy(CommodityEntity::getPosCode)
                .forEach(entry -> {
                    CommodityEntity entity = new CommodityEntity();
                    entity.setPosCode(entry.getKey());
                    entity.setFlag(true);
                    List<CommodityEntity> value = entry.getValue();
                    for (int i = 0; i < value.size(); i++) {
                        CommodityEntity commodityEntity = value.get(i);
                        entity.setPosID(commodityEntity.getPosID());
                        entity.setBarCode(commodityEntity.getBarCode());
                        entity.setCommodityId(commodityEntity.getCommodityId());
                        if (!commodityEntity.isFlag()) {
                            entity.setFlag(false);
                        }
                    }
                    list.add(entity);
                });
        List<CommodityEntity> entityList = Stream.ofNullable(list)
                .sortBy(CommodityEntity::getPosCode)
                .sorted((o1, o2) -> {
                    boolean o1Flag = o1.isFlag();
                    boolean o2Flag = o2.isFlag();
                    return Boolean.compare(o1Flag, o2Flag);
                })
                .toList();
        return entityList;
    }


    public ArrayList<CommodityEntity> getLitWjx() {
        return (ArrayList<CommodityEntity>) Stream.ofNullable(litWjx)
                .sortBy(CommodityEntity::getPosCode)
                .toList();
    }

    public void setLitWjx(ArrayList<CommodityEntity> litWjx) {
        this.litWjx = litWjx;
    }

    public ArrayList<CommodityEntity> getLitYjx() {
        return litYjx;
    }

    public void setLitYjx(ArrayList<CommodityEntity> litYjx) {
        this.litYjx = litYjx;
    }
}
